// const { User } = require('../model/user')
const User = require('../model/user2')
const { code } = require('../static/secret')
const jwt = require('jsonwebtoken')
const bcrypt = require('bcrypt')
module.exports = {
  loginService: async (username, password) => {
    const content = {}
    // const user = await User.findOne({ where: { username: username } })
    const user = await User.findOne({ username: username })
    if (!user) {
      content.status = 403
      content.message = '没有该用户'
      content.data = {}
      return content
    }
    if (await bcrypt.compare(password, user.password)) {
      content.status = 200
      content.message = '登陆成功'
      content.data = {}
      content.data.token = jwt.sign({ username: user.username }, code, { expiresIn: '1h' })
      content.data.role = user.role
    } else {
      content.status = 403
      content.message = '密码错误'
      content.data = {}
    }
    return content
  }
}
